﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QD.SyncServers
{
    public partial class SyncStep1 : BaseFrm
    {
        public SyncStep1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void SyncStep1_Load(object sender, EventArgs e)
        {

        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                string _servername = this.txtTargetServerName.Text.Trim();
                string _userid = this.txtTargetUserId.Text.Trim();
                string _password = this.txtTargetUserPassword.Text.Trim();

                if (string.IsNullOrEmpty(_servername))
                {
                    throw new Exception("请输入服务器名称(或IP)");
                }
                else if (string.IsNullOrEmpty(_userid))
                {
                    throw new Exception("请输入用户名");
                }
                else if (string.IsNullOrEmpty(_password))
                {
                    throw new Exception("请输入密码");
                }

                string connStr = "Data Source=" + _servername + ";User ID=" + _userid + ";Password=" + _password + ";Initial Catalog=master; persist security info=true";                
                SqlConnection conn = new SqlConnection(connStr);
                try
                {
                    conn.Open();
                    conn.Close();

                    ConfigInstanceHelper.ConfigInstance.TargetServerName = _servername;
                    ConfigInstanceHelper.ConfigInstance.TargetUserId = _userid;
                    ConfigInstanceHelper.ConfigInstance.TargetUserPassword = _password;
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                this.Hide();
                Form frm = Application.OpenForms["SyncStep2"];
                if (frm == null)
                {
                    frm = new SyncStep2();
                    frm.ShowDialog();
                }
                else
                {
                    frm.Show();
                }
            }
            catch (Exception ex)
            {
                Tools.PromptHelper.Prompt(ex);
            }
        }
    }
}
